""" 

    Charts.py used in RepresentedBy Facebook application
    http://apps.facebook.com/representedby/
    
    This script is not part of the active application, just a tool that was used to generate some of the earmark charts.
    
"""

__author__ = "Will Turnage (will@wubbahed.com)"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2009 wubbahed.com"
__license__ = "GPLv3"


import os
import wsgiref.handlers
import facebook
import const
import string

from sets import Set
from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
from google.appengine.ext import db
from utils import fbUtil, dbUtil, UserDistrict, SunlightUtil

class MainHandler(webapp.RequestHandler):
    
    def get(self):
        self.post()
        
    def post(self):
        

        earmarks_2008 = { "AK":{"state":"AK", "total":"$346,073,350", "percapita": "$506.34", "friends":[]}, "HI":{"state":"HI", "total":"$289,868,650", "percapita": "$225.86", "friends":[]}, "WV":{"state":"WV", "total":"$358,530,318", "percapita": "$197.86", "friends":[]}, "MS":{"state":"MS", "total":"$511,478,000", "percapita": "$175.24", "friends":[]}, "ND":{"state":"ND", "total":"$105,858,230", "percapita": "$165.48", "friends":[]}, "VT":{"state":"VT", "total":"$76,375,960", "percapita": "$122.94", "friends":[]}, "SD":{"state":"SD", "total":"$91,806,638", "percapita": "$115.30", "friends":[]}, "NM":{"state":"NM", "total":"$211,940,090", "percapita": "$107.59", "friends":[]}, "ID":{"state":"ID", "total":"$155,662,700", "percapita": "$103.82", "friends":[]}, "MT":{"state":"MT", "total":"$91,685,490", "percapita": "$95.72", "friends":[]}, "NV":{"state":"NV", "total":"$217,322,770", "percapita": "$84.71", "friends":[]}, "RI":{"state":"RI", "total":"$84,735,031", "percapita": "$80.10", "friends":[]}, "AR":{"state":"AR", "total":"$214,555,110", "percapita": "$75.69", "friends":[]}, "AL":{"state":"AL", "total":"$320,860,020", "percapita": "$69.33", "friends":[]}, "LA":{"state":"LA", "total":"$295,329,910", "percapita": "$68.79", "friends":[]}, "DE":{"state":"DE", "total":"$55,304,205", "percapita": "$63.95", "friends":[]}, "MO":{"state":"MO", "total":"$346,784,080", "percapita": "$58.99", "friends":[]}, "KS":{"state":"KS", "total":"$156,926,251", "percapita": "$56.53", "friends":[]}, "MD":{"state":"MD", "total":"$303,619,634", "percapita": "$54.04", "friends":[]}, "IA":{"state":"IA", "total":"$152,697,200", "percapita": "$51.10", "friends":[]}, "WA":{"state":"WA", "total":"$329,564,467", "percapita": "$50.95", "friends":[]}, "UT":{"state":"UT", "total":"$134,709,500", "percapita": "$50.92", "friends":[]}, "KY":{"state":"KY", "total":"$214,370,920", "percapita": "$50.54", "friends":[]}, "NH":{"state":"NH", "total":"$64,489,378", "percapita": "$49.01", "friends":[]}, "VA":{"state":"VA", "total":"$331,785,010", "percapita": "$43.02", "friends":[]}, "MN":{"state":"MN", "total":"$222,062,125", "percapita": "$42.72", "friends":[]}, "PA":{"state":"PA", "total":"$523,033,080", "percapita": "$42.07", "friends":[]}, "WY":{"state":"WY", "total":"$21,921,600", "percapita": "$41.93", "friends":[]}, "CT":{"state":"CT", "total":"$143,741,924", "percapita": "$41.04", "friends":[]}, "TN":{"state":"TN", "total":"$215,521,900", "percapita": "$35.01", "friends":[]}, "SC":{"state":"SC", "total":"$151,664,000", "percapita": "$34.41", "friends":[]}, "MA":{"state":"MA", "total":"$220,254,900", "percapita": "$34.15", "friends":[]}, "NJ":{"state":"NJ", "total":"$285,241,890", "percapita": "$32.84", "friends":[]}, "WI":{"state":"WI", "total":"$182,476,400", "percapita": "$32.58", "friends":[]}, "IN":{"state":"IN", "total":"$198,878,300", "percapita": "$31.34", "friends":[]}, "IL":{"state":"IL", "total":"$394,225,400", "percapita": "$30.67", "friends":[]}, "OK":{"state":"OK", "total":"$109,253,100", "percapita": "$30.20", "friends":[]}, "ME":{"state":"ME", "total":"$38,775,000", "percapita": "$29.44", "friends":[]}, "TX":{"state":"TX", "total":"$649,242,180", "percapita": "$27.16", "friends":[]}, "OH":{"state":"OH", "total":"$304,805,730", "percapita": "$26.58", "friends":[]}, "FL":{"state":"FL", "total":"$477,044,050", "percapita": "$26.14", "friends":[]}, "NC":{"state":"NC", "total":"$234,524,550", "percapita": "$25.88", "friends":[]}, "NE":{"state":"NE", "total":"$45,701,640", "percapita": "$25.75", "friends":[]}, "CA":{"state":"CA", "total":"$929,217,800", "percapita": "$25.42", "friends":[]}, "OR":{"state":"OR", "total":"$95,200,610", "percapita": "$25.40", "friends":[]}, "MI":{"state":"MI", "total":"$229,800,600", "percapita": "$22.82", "friends":[]}, "NY":{"state":"NY", "total":"$437,239,735", "percapita": "$22.66", "friends":[]}, "GA":{"state":"GA", "total":"$214,905,300", "percapita": "$22.52", "friends":[]}, "CO":{"state":"CO", "total":"$91,835,710", "percapita": "$18.89", "friends":[]}, "AZ":{"state":"AZ", "total":"$118,554,400", "percapita": "$18.70", "friends":[]} }
        earmarks_2009 = {"AK": {"state": "AK", "percapita": "$209.71", "total": "$143,922,875", "friends":[]}, "ND": {"state": "ND", "percapita": "$172.55", "total": "$110,687,091", "friends":[]}, "HI": {"state": "HI", "percapita": "$128.12", "total": "$165,048,707", "friends":[]}, "MS": {"state": "MS", "percapita": "$110.59", "total": "$324,976,918", "friends":[]}, "SD": {"state": "SD", "percapita": "$106.71", "total": "$85,818,750", "friends":[]}, "WV": {"state": "WV", "percapita": "$97.40", "total": "$176,737,900", "friends":[]}, "VT": {"state": "VT", "percapita": "$84.29", "total": "$52,367,530", "friends":[]}, "MT": {"state": "MT", "percapita": "$79.61", "total": "$77,022,250", "friends":[]}, "NM": {"state": "NM", "percapita": "$72.61", "total": "$144,092,821", "friends":[]}, "AR": {"state": "AR", "percapita": "$55.72", "total": "$159,114,268", "friends":[]}, "LA": {"state": "LA", "percapita": "$52.78", "total": "$232,822,813", "friends":[]}, "AL": {"state": "AL", "percapita": "$51.52", "total": "$240,167,157", "friends":[]}, "MN": {"state": "MN", "percapita": "$38.13", "total": "$199,031,643", "friends":[]}, "IA": {"state": "IA", "percapita": "$37.90", "total": "$113,803,486", "friends":[]}, "DE": {"state": "DE", "percapita": "$36.97", "total": "$32,275,820", "friends":[]}, "RI": {"state": "RI", "percapita": "$35.84", "total": "$37,662,822", "friends":[]}, "NV": {"state": "NV", "percapita": "$34.36", "total": "$89,345,369", "friends":[]}, "UT": {"state": "UT", "percapita": "$34.31", "total": "$93,891,425", "friends":[]}, "KY": {"state": "KY", "percapita": "$32.91", "total": "$140,482,257", "friends":[]}, "WY": {"state": "WY", "percapita": "$27.25", "total": "$14,516,350", "friends":[]}, "ID": {"state": "ID", "percapita": "$27.10", "total": "$41,292,139", "friends":[]}, "WA": {"state": "WA", "percapita": "$26.22", "total": "$171,723,572", "friends":[]}, "NH": {"state": "NH", "percapita": "$25.78", "total": "$33,921,987", "friends":[]}, "MD": {"state": "MD", "percapita": "$23.47", "total": "$132,202,990", "friends":[]}, "MO": {"state": "MO", "percapita": "$23.15", "total": "$136,853,741", "friends":[]}, "KS": {"state": "KS", "percapita": "$23.13", "total": "$64,814,273", "friends":[]}, "MA": {"state": "MA", "percapita": "$23.07", "total": "$149,910,169", "friends":[]}, "OK": {"state": "OK", "percapita": "$22.48", "total": "$81,862,200", "friends":[]}, "VA": {"state": "VA", "percapita": "$19.97", "total": "$155,164,892", "friends":[]}, "OR": {"state": "OR", "percapita": "$19.86", "total": "$75,251,925", "friends":[]}, "PA": {"state": "PA", "percapita": "$19.46", "total": "$242,303,580", "friends":[]}, "WI": {"state": "WI", "percapita": "$18.30", "total": "$102,978,050", "friends":[]}, "MI": {"state": "MI", "percapita": "$17.94", "total": "$179,472,590", "friends":[]}, "NJ": {"state": "NJ", "percapita": "$17.35", "total": "$150,670,750", "friends":[]}, "CT": {"state": "CT", "percapita": "$16.63", "total": "$58,231,874", "friends":[]}, "OH": {"state": "OH", "percapita": "$16.32", "total": "$187,423,884", "friends":[]}, "TN": {"state": "TN", "percapita": "$16.23", "total": "$100,880,000", "friends":[]}, "SC": {"state": "SC", "percapita": "$15.83", "total": "$70,894,872", "friends":[]}, "CA": {"state": "CA", "percapita": "$15.47", "total": "$568,730,493", "friends":[]}, "TX": {"state": "TX", "percapita": "$15.21", "total": "$370,046,650", "friends":[]}, "ME": {"state": "ME", "percapita": "$15.15", "total": "$19,947,000", "friends":[]}, "NE": {"state": "NE", "percapita": "$14.99", "total": "$26,733,050", "friends":[]}, "IN": {"state": "IN", "percapita": "$14.12", "total": "$90,040,310", "friends":[]}, "FL": {"state": "FL", "percapita": "$13.74", "total": "$251,823,596", "friends":[]}, "NC": {"state": "NC", "percapita": "$12.88", "total": "$118,796,030", "friends":[]}, "NY": {"state": "NY", "percapita": "$12.57", "total": "$245,083,122", "friends":[]}, "IL": {"state": "IL", "percapita": "$10.93", "total": "$140,965,682", "friends":[]}, "CO": {"state": "CO", "percapita": "$9.48", "total": "$46,818,025", "friends":[]}, "GA": {"state": "GA", "percapita": "$9.31", "total": "$90,163,019", "friends":[]}, "AZ": {"state": "AZ", "percapita": "$8.41", "total": "$54,689,096", "friends":[]} }

        results = {"2008_states": "", "2008_colors":"", "2009_states": "", "2009_colors":""}
        states = earmarks_2008.keys()
        vals2008 = {}
        vals2009 = {}
        sorted2008 = []
        sorted2009 = []
        for state in states:
            val2008 = float(earmarks_2008[state]["percapita"].lstrip("$")) * 1000
            sorted2008.append(val2008)
            vals2008[val2008] = earmarks_2008[state]
            val2009 = float(earmarks_2009[state]["percapita"].lstrip("$")) * 1000
            sorted2009.append(val2009)
            vals2009[val2009] = earmarks_2009[state]
        
        sorted2008.sort(reverse=True)
        sorted2009.sort(reverse=True)
        
        for counter in range(50):
            results["2008_states"] = results["2008_states"] + vals2008[sorted2008[counter]]["state"]
            results["2008_colors"] = results["2008_colors"] + str(counter*2) + "," 
            results["2009_states"] = results["2009_states"] + vals2009[sorted2009[counter]]["state"]
            results["2009_colors"] = results["2009_colors"] + str(counter*2) + "," 
            
        results["2008_colors"] = str.rstrip(results["2008_colors"], ",")  
        results["2009_colors"] = str.rstrip(results["2009_colors"], ",")  
        path = os.path.join(os.path.dirname(__file__), '../templates/charts.fbml')
        self.response.out.write(template.render(path, results ))
            

            
  
def main():
	application = webapp.WSGIApplication([('/fb/charts', MainHandler)],
									   debug=True)
	run_wsgi_app(application)

# load our custom filters		   
webapp.template.register_template_library('customfilters')

if __name__ == '__main__':
	main()
